require.config({
    paths: {
        u: '../../utils'
    }
});

require([
     'u/common/app',
	 'u/common/route',
	 'u/com/lm_load_more'
], function() {
  $(function(){
 
	if($(".hema_ad").length>0){
		var show=$(".hema_ad").attr("data-show")
		var click=$(".hema_ad").attr("data-click")
		$.get(show)
		$(".hema_ad").click(function(){
			var url=$(this).attr("href")
			$.ajax({
				url: click,
				type: "GET",
				dataType: "json",
				success: function(data) {
				   window.location.href=url
				}
			  })
		})
	  }
	//加载更多评论
	 if(parseInt($(".comment_bar .right em").text())==0){
	 	$(".comment_bar .right em").hide()
	 }
	 else
	 {
	 	$(".comment_bar .right em").show()
	 }
	 var load_more=new lm_load_more({
			   ele:$(".lm_load_more"),
			   list:$("[listmore]"),
			   success:function(e,page){
				   var Data=e
				   var timer=null
				   if(Data.rescode==0){
						timer=setTimeout(function(){
							clearTimeout(timer)
							doajax(e)
						},1000)
				   }
				   else
				   {
					  KZ.form.Prompt(Data.resmsg,1)
					}
					function doajax(e){
						if (e.hasMore == false) {
							 $(load_more.ele).attr("data-page",0);
							 $(Data.html).appendTo(load_more.list);
							 $(load_more.ele).find(".loading").html("“别拖了，下面真没了”")
						} 
						else 
						{
							 $(load_more.ele).attr("data-page",page+1)
							 $(Data.html).appendTo(load_more.list);
						}
					}

			   }
		})
	  //长按评论
	 var userinfo={}
     $(".chat_list").each(function(index, element) {
		 var T=$(this);
		 var li=T.find("li");
		 var vote=T.find(".vote");
		 var num=vote.find("em")
		 var timeOutEvent=0;
		   $(T).unbind("touchstart").delegate("li","touchstart",function(){
				  var _self=$(this);
				  //app长按弹出层
		            timeOutEvent = setTimeout(function(){longPress(_self)},200);
		   })
		    $(T).unbind("touchmove").delegate("li","touchmove",function(){
		            clearTimeout(timeOutEvent); 
					timeOutEvent = 0; 
		   })
		     $(T).unbind("touchend").delegate("li","touchend",function(e){
		            clearTimeout(timeOutEvent); 
					timeOutEvent = 0; 
					e.stopPropagation()
		   })
		    $(T).unbind("click").delegate("li","click",function(){
		           clearTimeout(timeOutEvent); 
				  timeOutEvent = 0; 
		   })
		   $(T).unbind("click").delegate(".vote","click",function(){
			      var _this=$(this)
				  if(!_this.find(".hm_icon").hasClass("cur")){
					  $.ajax({
						url: _init.vote,
						data:{id:_this.parent().parent().data("id")},
						type: "POST",
						dataType: "json",
						success: function(data) {
							KZ.form.Prompt(data.resmsg,1);
							if(data.rescode==0){
								_this.find("em").text(parseInt(_this.find("em").text())+1)
								_this.find(".hm_icon").addClass("cur")
							}
						},
						error: function(er) {
							BackErr(er);
						}
					});
				}
				else
				{
					KZ.form.Prompt("不能投了",1);
				}
		          clearTimeout(timeOutEvent); 
				  timeOutEvent = 0; 
		   })
		function longPress(_self){
			userinfo={
				id:_self.data("id")
			}
			KZ.pageLock.show(1000,"#hm_long","");
			$("#hm_long .cancel").on("touchstart",function(){
				KZ.pageLock.reHide("#hm_long")
			})
		}
	 });
	 //点击评论路由到评论页
	 var dom = {
            init: function() {
                  $(".comment_fabu").hide();
				  $(".main").show();
				  $("body").css({"margin-bottom":"4rem"})
            },
			action:function(){
				  $(".comment_fabu").show();
				  $(".main").hide();
				  $("body").css({"margin-bottom":0}).addClass("none")
			},
			getuser:function(info){
				   $(".comment_fabu").find(".replyId").val(info.id)
			},
			add_comment:function(e,o){
				//新增评论
				$(e.html).prependTo(".chat_list ul")
				KZ.form.Prompt(e.resmsg,1);
				$("#comment_fabu")[0].reset()
				window.location.href="#/";
				$(".comment_bar .right em").text(parseInt($(".comment_bar .right em").text())+1);
				$(".comment_bar .right em").show();
				setTimeout(function(){$("body,html").animate({"scrollTop":$(".chat_list").position().top-120},0)},0)
			},
			islogin:function(url,callback){
				  $.ajax({
					url: url,
					type: "post",
					dataType: "json",
					success: function(data) {
						if(data.rescode==0){
							callback(data)
						}
						else
						{
							//登陆失败
							//$("<iframe src="+data.url+" id='appStart' ></iframe>").appendTo("body");
							 KZ.form.Prompt(data.resmsg,1)
						}
					},
					error: function(er) {
						BackErr(er);
					}
				});
				
			}
     }
     $(".comment_bar .input").click(function(){
	 	   var T=$(this);
	 	   var url=$(".comment_bar .right a").attr("href");
		    dom.islogin(_init.login,function(e){
		    	  if(e.isLogin){
					  window.location.href=url;
				  }
		    	  if(!e.isLogin){
		    	  	 KZ.pageLock.reHide("#hm_long");
					 window.location.href="hippo://hippo.app/openwith?type=login";
					
				  }
				  //如果点击的是回复
			
				  if(T.hasClass("huifu")){
					  dom.getuser(userinfo)
				  }
			})
		    return false
	 })
	 $(".hm_layer .huifu").click(function(){
	 	   var T=$(this);
		    dom.islogin(_init.login,function(e){
		    	  if(e.isLogin){
					  window.location.href="#/comment";
					  KZ.pageLock.reHide("#hm_long");
					  $(".cancel").click()
				  }
		    	  if(!e.isLogin){
		    	  	 KZ.pageLock.reHide("#hm_long");
					 window.location.href="hippo://hippo.app/openwith?type=login";
					
				  }
				  //如果点击的是回复
			
				  if(T.hasClass("huifu")){
					  dom.getuser(userinfo)
				  }
			})
		    return false
	 })
	 //举报
	 $(".hm_layer .jubao").click(function(){
		   $.ajax({
				url: _init.jubao,
				data:{id:userinfo.id},
				type: "get",
				dataType: "json",
				success: function(data) {
					KZ.form.Prompt(data.resmsg,1);
					KZ.pageLock.reHide("#hm_long");
					//$(".cancel").click()
				},
				error: function(er) {
					BackErr(er);
				}
			});
	 })
	 //收藏
	 $(".hm_icon.b2").click(function(){
		  var _this=$(this);
		  var url=_this.data("url");
		  var url2=_this.data("url2");
		  dom.islogin(_init.login,function(e){
	    	  if(!e.isLogin){
				 window.location.href="hippo://hippo.app/openwith?type=login";
			  }
	    	  else
	    	  {
	    	  	 if(!_this.hasClass("disable")){
		    	  	$.ajax({
						url: url,
						type: "get",
						dataType: "json",
						success: function(data) {
							KZ.form.Prompt(data.resmsg,1)
							if(data.rescode==0){
								_this.addClass("disable")
							}
						},
						error: function(er) {
							BackErr(er);
						}
					});
				}
	    	  	 else
	    	  	 {
	    	  	 		$.ajax({
						url: url2,
						type: "get",
						dataType: "json",
						success: function(data) {
							KZ.form.Prompt(data.resmsg,1)
							if(data.rescode==0){
								_this.removeClass("disable")
							}
						},
						error: function(er) {
							BackErr(er);
						}
					});
	    	  	 }
	    	  	 
	    	  }
			   
		  })
	  })
	  //发布评论
	 $(".comment_fabu").each(function(index, element) {
		 var T=$(this);
		 var btn=T.find(".btn_fabu");
		 var content=T.find("textarea");

		 btn.on("click",function(){
			 var text=T.find("textarea").val();
			 var from=$("#comment_fabu");
			 if(text.length==0){
				   KZ.form.Prompt("评论内容不能为空",1)
				   return false
			  }
			  if(text.length>=2 && text.length<140){
				  dom.islogin(_init.login,function(e){
				  	if(!e.isLogin){
						window.location.href="hippo://hippo.app/openwith?type=login";
					 }
				  	else
				  	{
				  		$.ajax({
							url: from.attr("action"),
							data:from.serialize(),
							type: "POST",
							dataType: "json",
							success: function(data) {
								if(data.rescode==0){
									dom.add_comment(data,from)
								}
								else
								{
									KZ.form.Prompt(data.resmsg,1)
								}
							},
							error: function(er) {
								BackErr(er);
							}
						});
				  		
				  	}
			    	
					 
				})
			  }
			  else
			  {
				   KZ.form.Prompt("评论内容长度2-140个字",1)
				   return false
			  }
			return false
		 })
	 });
	 var app = new Xroute();
		app.get('/:i', function(req, res, next) {
			var i = req.param.i
			if (i == "comment") {
				 dom.action()
			}
			if (i == "") {
				 dom.init()
			}
			//页面初始化
			app.get(function(req, res, next) {
				dom.init()
			});
		 });
	 app.start();
		 
  })
})
